import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:flutter/cupertino.dart';

/// AES加密算法
class AES {
  static String encryptAes({
    required String plainText,
    required String keyStr,
    required String ivStr,
    encrypt.AESMode? mode = encrypt.AESMode.cbc,
  }) {
    final key = encrypt.Key.fromUtf8(keyStr);
    final iv = encrypt.IV.fromUtf8(ivStr);
    final encrypter =
        encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));

    final encrypted = encrypter.encrypt(plainText, iv: iv);
    debugPrint(encrypted.base64); // 加密的结果

    return encrypted.base64;
  }

  static String decryptAes(
      {required String encryptedStr,
      required String keyStr,
      required String ivStr,
      encrypt.AESMode? mode = encrypt.AESMode.cbc}) {
    final key = encrypt.Key.fromUtf8(keyStr);
    final iv = encrypt.IV.fromUtf8(ivStr);

    final encrypter =
        encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
    final decrypted = encrypter.decrypt64(encryptedStr, iv: iv);

    return decrypted;
  }
}
